package com.ruoyi.project.channel.smsInfo.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.AddressUtils;
import com.ruoyi.common.utils.SMSUtil;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.pay.mail.IMailService;
import com.ruoyi.project.system.user.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.channel.smsInfo.mapper.SmsInfoMapper;
import com.ruoyi.project.channel.smsInfo.domain.SmsInfo;
import com.ruoyi.common.utils.text.Convert;

/**
 * 短信 服务层实现
 * 
 * @author CAIN
 * @date 2019-10-26
 */
@Service
public class SmsInfoServiceImpl implements ISmsInfoService 
{
	@Autowired
	private SmsInfoMapper smsInfoMapper;
	@Autowired
	private IMailService mailService;
	private static final Logger log = LoggerFactory.getLogger(SmsInfoServiceImpl.class);
	/**
     * 查询短信信息
     * 
     * @param smsId 短信ID
     * @return 短信信息
     */
    @Override
	public SmsInfo selectSmsInfoById(Long smsId)
	{
	    return smsInfoMapper.selectSmsInfoById(smsId);
	}
	
	/**
     * 查询短信列表
     * 
     * @param smsInfo 短信信息
     * @return 短信集合
     */
	@Override
	public List<SmsInfo> selectSmsInfoList(SmsInfo smsInfo)
	{
	    return smsInfoMapper.selectSmsInfoList(smsInfo);
	}
	
    /**
     * 新增短信
     * 
     * @param smsInfo 短信信息
     * @return 结果
     */
	@Override
	public int insertSmsInfo(SmsInfo smsInfo)
	{
	    return smsInfoMapper.insertSmsInfo(smsInfo);
	}
	
	/**
     * 修改短信
     * 
     * @param smsInfo 短信信息
     * @return 结果
     */
	@Override
	public int updateSmsInfo(SmsInfo smsInfo)
	{
	    return smsInfoMapper.updateSmsInfo(smsInfo);
	}

	/**
     * 删除短信对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteSmsInfoByIds(String ids)
	{
		return smsInfoMapper.deleteSmsInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * 发送验证码
	 * @param user
	 */
	@Override
	public AjaxResult doSendCode(User user){
		Date date = new Date(); //获取当前的系统时间。
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm"); //使用了默认的格式创建了一个日期格式化对象。
		String time = dateFormat.format(date);
		String ip = ServletUtils.getRequest().getRemoteHost();
//		String address = "XX XX";
//		try {
//			address = AddressUtils.getRealAddressByIP(ip);
//		} catch (Exception e) {
//		}

		String phoneNum = user.getPhonenumber();
		SmsInfo searchSms = new SmsInfo();
		searchSms.setPhoneNum(phoneNum);
		searchSms.setUserId(user.getUserId());
		searchSms.getParams().put("time", "30");
		List<SmsInfo> smsInfos = selectSmsInfoList(searchSms);
		if (smsInfos.size() == 0) {
			// 把这个号码的验证码都设置为不可用
			SmsInfo searchInfo = new SmsInfo();
			searchInfo.setUseStatus("0");
			searchInfo.setPhoneNum(phoneNum.trim());
			List<SmsInfo> smsInfos1 = selectSmsInfoList(searchInfo);
			for (int i = 0; i < smsInfos1.size(); i++) {
				SmsInfo t = smsInfos1.get(i);
				t.setUseStatus("1");
				updateSmsInfo(t);
			}

			String verifyCode = String
					.valueOf(new Random().nextInt(899999) + 100000);
			// 开始发送验证短信
			String content = "[ASA] your verification code: "+verifyCode+", valid for 10 minutes, please do not disclose.";
			JSONObject jsonObject = SMSUtil.sendSms(content, phoneNum.trim());
			if(jsonObject.getString("code").equals("0")){
				SmsInfo smsInfo = new SmsInfo();
				smsInfo.setPhoneNum(phoneNum.trim());
				smsInfo.setSmsCode(verifyCode);
				smsInfo.setSmsType("登录验证码");
				smsInfo.setUserId(user.getUserId());
				smsInfo.setUseStatus("0");
				smsInfo.setRemark(ip);
				smsInfo.setCreateTime(new Date());
				insertSmsInfo(smsInfo);
				return AjaxResult.success("success");
			}
		}
		return AjaxResult.error("error");
	}

	/**
	 * 校验验证码
	 * @param phoneNum
	 *  @param phoneCode
	 */
	@Override
	public boolean validateCode(String  phoneNum,String phoneCode){
		boolean b = false;
		if (phoneCode == null || phoneCode.trim().equals("")) {
			return b;
		}
		SmsInfo searchInfo = new SmsInfo();
		searchInfo.setPhoneNum(phoneNum.trim());
		searchInfo.setSmsCode(phoneCode.trim());
		searchInfo.setUseStatus("0");
		searchInfo.getParams().put("time", "30");
		List<SmsInfo> smsInfos = selectSmsInfoList(searchInfo);
		for (int i = 0; i < smsInfos.size(); i++) {
			SmsInfo smsInfo_ = smsInfos.get(i);
			if (smsInfo_.getSmsCode().equals(phoneCode.trim())) {
				b = true;
				break;
			}
		}
		return  b;
	}
}
